﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CJJCH;
using System.Web.UI.HtmlControls;

namespace CJJCH.Web.Manage
{
    public partial class NewsList : PowerBase
    {
        CJJCH.BLL.T_NET_News bll_news = new CJJCH.BLL.T_NET_News();
        CJJCH.BLL.T_NET_MenuThird bll_menuThird = new CJJCH.BLL.T_NET_MenuThird();
        CJJCH.BLL.T_NET_MenuSecond bll_menuSecond = new CJJCH.BLL.T_NET_MenuSecond();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
            }
        }
        private void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].Trim()))
            {
                CJJCH.Model.T_NET_MenuSecond model = new CJJCH.Model.T_NET_MenuSecond();
                model = bll_menuSecond.GetMenuSecondModel(Convert.ToInt32(Request.QueryString["secondid"].ToString()));
                if (model!=null)
                {
                    this.lblSecondID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["secondid"].Trim());
                    this.NewsTop1.SecondId = Convert.ToInt32(this.lblSecondID.Text.Trim());
                    this.lblSecondName.Text = model.MenuName;
                    this.NewsTop1.SecondName = this.lblSecondName.Text.Trim();
                    if (!model.HaveChild)
                    {
                        this.tdType.Style.Add("display","none;");
                        this.NewsTop1.TypeID = 0;
                        this.thType.Style.Add("display","none;");                       
                    }
                    else
                    {
                        this.NewsTop1.TypeID = 1;
                        IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                        list = bll_menuThird.GetDrop(Convert.ToInt32(this.lblSecondID.Text.Trim()));
                        int typeCount = list.Count;
                        if (typeCount > 0)
                        {
                            for (int i = 0; i < typeCount; i++)
                            {
                                dropNewsType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                            }
                            list = null;
                        }
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }            
        }
        private void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
                txtKeyword.Text = lblKeyword.Text;
            }
            if (Request.QueryString["orderkey"] != null)
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (Request.QueryString["ordertype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["ordertype"].Trim()))
            {
                lblOrderType.Text = Request.QueryString["ordertype"].Trim();
            }
            if (Request.QueryString["thirdid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["thirdid"].Trim()))
            {
                lblThirdID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["thirdid"].Trim());
                dropNewsType.SelectedValue = lblThirdID.Text.Trim();
            }
            else
            {
                lblThirdID.Text = "0";
                dropNewsType.SelectedValue = lblThirdID.Text.Trim();
            }
            if (Request.QueryString["betime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblBeTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
                this.txtBegin.Text = lblBeTime.Text.Trim();
            }
            if (Request.QueryString["edtime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblEdTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
                this.txtEnd.Text = lblEdTime.Text.Trim();
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].ToString()))
            {
                int curPage = int.Parse(Request.QueryString["curpage"].ToString());
                AspNetPager1.RecordCount = curPage * AspNetPager1.PageSize;
                AspNetPager1.CurrentPageIndex = curPage;
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
            }
        }
        private void RepeaterDataBinding()
        {
            string beginTime = "";
            try
            {
                beginTime = Convert.ToDateTime(lblBeTime.Text.Trim()).ToString();
            }
            catch{ }
            string endTime = "";
            try
            {
                endTime = Convert.ToDateTime(lblEdTime.Text.Trim()).AddDays(1).ToString();
            }
            catch { }
            string strOrder = "";
            if (lblOrderKey.Text.Trim() != "")
            {
                strOrder += lblOrderKey.Text.Trim();
                if (lblOrderType.Text.Trim() == "1")
                {
                    strOrder += " desc,";
                }
                else
                {
                    strOrder += " asc,";
                }
            }
            int ThirdID = 0;
            try
            {
                ThirdID = Convert.ToInt32(this.lblThirdID.Text.Trim());
            }
            catch{ }
            
            int totalCount = 0;
            IList<CJJCH.Model.T_NET_News> list = new List<CJJCH.Model.T_NET_News>();
            string dataTitle = LTP.Common.StringClass.EncodeString(this.txtKeyword.Text.Trim());
            int secondid = Convert.ToInt32(this.lblSecondID.Text.ToString());
            list = bll_news.GetNewsList(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, secondid, dataTitle, ThirdID, beginTime, endTime, ref totalCount, strOrder);
            AspNetPager1.RecordCount = totalCount > (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize ? totalCount : AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize;
            rptNews.DataSource = list;
            rptNews.DataBind();
            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";
            AspNetPager1.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + totalCount.ToString() + "</b></font>条记录";
            AspNetPager1.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager1.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
        }
        protected void rptNewsType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((LinkButton)e.Item.FindControl("lbtnTitle")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).DataTitle;
                ((LinkButton)e.Item.FindControl("lbtnTitle")).Attributes.Add("onclick", "ShowFinish('" + ((CJJCH.Model.T_NET_News)e.Item.DataItem).ID.ToString() + "');return false;");
                if (!((CJJCH.Model.T_NET_News)e.Item.DataItem).MenuSecond.HaveChild)
                {
                    ((HtmlTableCell)e.Item.FindControl("tdType")).Style.Add("display", "none;");                    
                }
                else
                {
                    ((Label)e.Item.FindControl("lblType")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).MenuThird.MenuName;
                }
                ((Label)e.Item.FindControl("lblPlusDate")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).PlusDate.ToString("yyyy/MM/dd");
                ((Label)e.Item.FindControl("lblHits")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).Hits.ToString();
                ((Label)e.Item.FindControl("lblOrderNum")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).OrderNum.ToString();
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_News)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_News)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该数据吗？')){return true;}else{return false;}");                
            }
        }
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                Response.Redirect("NewsEdit.aspx?ID="
                    + LTP.Common.StringClass.EncodeUrl(((LinkButton)sender).CommandArgument.ToString())
                    + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondID.Text.Trim())
                    + "&thirdid=" + LTP.Common.StringClass.EncodeUrl(this.lblThirdID.Text.Trim())
                    + "&curpage=" + LTP.Common.StringClass.EncodeUrl(AspNetPager1.CurrentPageIndex.ToString())
                    + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                    + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                    + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                    + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                    + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim()));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("错误操作！");
            }
        }
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                string error = bll_news.DelNews(new Guid(((LinkButton)sender).CommandArgument));
                if (error == "")
                {
                    Response.Redirect("NewsList.aspx?secondid=" 
                        + LTP.Common.StringClass.EncodeUrl(lblSecondID.Text.Trim())
                        + "&curpage=" + LTP.Common.StringClass.EncodeUrl(AspNetPager1.CurrentPageIndex.ToString())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&thirdid=" + LTP.Common.StringClass.EncodeUrl(this.lblThirdID.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblKeyword.Text = LTP.Common.StringClass.EncodeString(txtKeyword.Text.Trim());
            lblBeTime.Text = LTP.Common.StringClass.EncodeString(txtBegin.Text.Trim());
            lblEdTime.Text = LTP.Common.StringClass.EncodeString(txtEnd.Text.Trim());
            this.lblThirdID.Text = LTP.Common.StringClass.EncodeString(dropNewsType.SelectedValue.ToString());
           
            AspNetPager1.CurrentPageIndex = 1;
        }
        #region 标头排序功能

        protected void lbtnTitle_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = " N.DataTitle ";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        //protected void lbtnType_Click(object sender, EventArgs e)
        //{
        //    lblOrderKey.Text = " N.ThirdID ";
        //    if (lblOrderType.Text == "0")
        //    {
        //        lblOrderType.Text = "1";
        //    }
        //    else
        //    {
        //        lblOrderType.Text = "0";
        //    }
        //    AspNetPager1.CurrentPageIndex = 1;
        //}

        protected void lbtnHits_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = " N.Hits ";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        #endregion
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            RepeaterDataBinding();
        }
    }
}